home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP05.ZIP / CHAP05 / PATRON / PAGES.CPP < prev    next >
C/C++ Source or Header  |  1993-06-07  |  23KB  |  1,021 lines

  1. /*
  2.  * PAGES.CPP
  3.  * Modifications for Chapter 5
  4.  *
  5.  * Implementation of the CPages class.  See PAGEWIN.CPP for additional
  6.  * member functions.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18.  
  19. #include "patron.h"
  20.  
  21.  
  22. HWND        g_hDlgPrint=NULL;
  23. BOOL        g_fCancelPrint=FALSE;
  24.  
  25.  
  26. /*
  27.  * CPages:CPages
  28.  * CPages::~CPages
  29.  *
  30.  * Constructor Parameters:
  31.  *  hInst           HINSTANCE of the application we're in.
  32.  */
  33.  
  34. CPages::CPages(HINSTANCE hInst)
  35.     : CWindow(hInst)
  36.     {
  37.     m_cPages=0;
  38.     m_iPageCur=0xFFFF;  //Pages are 0 indexed, so this is one before that.
  39.     m_hWndPageList=NULL;
  40.  
  41.     /*
  42.      * Initialize to 2.54cm*2.54cm which is a page with no space for anything,
  43.      * just margins.  2.54cm=.5 inches on each margin.
  44.      */
  45.     m_cx=LOMETRIC_PER_INCH;
  46.     m_cy=LOMETRIC_PER_INCH;
  47.  
  48.     m_xPos=0L;
  49.     m_yPos=0L;
  50.  
  51.     m_dwIDNext=0;
  52.  
  53.     //CHAPTER5MOD
  54.     m_pIStorage=NULL;
  55.     //End CHAPTER5MOD
  56.  
  57.     return;
  58.     }
  59.  
  60.  
  61. CPages::~CPages(void)
  62.     {
  63.     //CHAPTER5MOD
  64.     //Insure memory is cleaned up in list, and do final IStorage::Release
  65.     FIStorageSet(NULL, FALSE, FALSE);
  66.     //End CHAPTER5MOD
  67.  
  68.     if (NULL!=m_hFont && !m_fSystemFont)
  69.         DeleteObject(m_hFont);
  70.  
  71.     if (NULL!=m_hWndPageList)
  72.         DestroyWindow(m_hWndPageList);
  73.  
  74.     return;
  75.     }
  76.  
  77.  
  78.  
  79.  
  80.  
  81. /*
  82.  * CPages::FInit
  83.  *
  84.  * Purpose:
  85.  *  Instantiates a pages window within a given parent.  The
  86.  *  parent may be a main application window, could be an MDI child
  87.  *  window. We really do not care.
  88.  *
  89.  * Parameters:
  90.  *  hWndParent      HWND of the parent of this window
  91.  *  pRect           LPRECT that this window should occupy
  92.  *  dwStyle         DWORD containing the window's style flags.  Should
  93.  *                  contain WS_CHILD | WS_VISIBLE in typical circumstances.
  94.  *  uID             UINT ID to associate with this window
  95.  *  pv              LPVOID unused for now.
  96.  *
  97.  * Return Value:
  98.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  99.  */
  100.  
  101. BOOL CPages::FInit(HWND hWndParent, LPRECT pRect, DWORD dwStyle
  102.     , UINT uID, LPVOID pv)
  103.     {
  104.     int     cy;
  105.  
  106.     m_hWnd=CreateWindowEx(WS_EX_NOPARENTNOTIFY, SZCLASSPAGES
  107.         , SZCLASSPAGES, dwStyle, pRect->left, pRect->top
  108.         , pRect->right-pRect->left, pRect->bottom-pRect->top
  109.         , hWndParent, (HMENU)uID, m_hInst, (LPVOID)this);
  110.  
  111.     if (NULL==m_hWnd)
  112.         return FALSE;
  113.  
  114.     /*
  115.      * Create the hidden listbox we'll use to track pages.  We give it
  116.      * the owner-draw style so we can just store pointers in it.
  117.      */
  118.     m_hWndPageList=CreateWindow("listbox", "Page List", WS_POPUP | LBS_OWNERDRAWFIXED
  119.         , 0, 0, 100, 100, HWND_DESKTOP, NULL, m_hInst, NULL);
  120.  
  121.     if (NULL==m_hWndPageList)
  122.         return FALSE;
  123.  
  124.     //Create a 14 point Arial font, or use the system variable font.
  125.     cy=MulDiv(-14, LOMETRIC_PER_INCH, 72);
  126.     m_hFont=CreateFont(cy, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE
  127.         , ANSI_CHARSET, OUT_TT_PRECIS, CLIP_TT_ALWAYS, PROOF_QUALITY
  128.         , VARIABLE_PITCH | FF_SWISS, "Arial");
  129.  
  130.     if (NULL==m_hFont)
  131.         {
  132.         m_hFont=(HFONT)GetStockObject(ANSI_VAR_FONT);
  133.         m_fSystemFont=TRUE;
  134.         }
  135.  
  136.     return TRUE;
  137.     }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. //CHAPTER5MOD
  145.  
  146. /*
  147.  * CPages::FIStorageSet
  148.  *
  149.  * Purpose:
  150.  *  Provides the document's IStorage to the pages for its own uses.
  151.  *  If this is a new storage, then we initalize it with streams we
  152.  *  want to always exists.  If this is an open, then we create
  153.  *  our page list from the PageList string we wrote before.
  154.  *
  155.  * Parameters:
  156.  *  pIStorage       LPSTORAGE to the new or opened storage.  If this is
  157.  *                  NULL then we just clean up and exit.
  158.  *  fChange         BOOL indicating is this was a Save As operation
  159.  *                  meaning that we have the structure already, we
  160.  *                  just need to change our value of m_pIStorage.
  161.  *  fInitNew        BOOL indicating if this is a new storage or one
  162.  *                  opened from a previous save.
  163.  *
  164.  * Return Value:
  165.  *  None
  166.  */
  167.  
  168. BOOL CPages::FIStorageSet(LPSTORAGE pIStorage, BOOL fChange, BOOL fInitNew)
  169.     {
  170.     DWORD           dwMode=STGM_DIRECT | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
  171.     HRESULT         hr;
  172.     LPPAGE          pPage;
  173.     BOOL            fRet=FALSE;
  174.     ULONG           cbRead;
  175.     PAGELIST        pgList;
  176.     LPSTREAM        pIStream;
  177.     LPMALLOC        pIMalloc;
  178.     LPDWORD         pdwID;
  179.     UINT            i;
  180.  
  181.     //If we're just changing saved roots, just open the current page again
  182.     if (fChange)
  183.         {
  184.         if (NULL==pIStorage)
  185.             return FALSE;
  186.  
  187.         m_pIStorage->Release();
  188.         m_pIStorage=pIStorage;
  189.         m_pIStorage->AddRef();
  190.  
  191.         FPageGet(m_iPageCur, &pPage, TRUE);
  192.         return TRUE;
  193.         }
  194.  
  195.     if (NULL!=m_hWndPageList)
  196.         {
  197.         //On new or open, clean out whatever it is we have.
  198.         for (i=0; i < m_cPages; i++)
  199.             {
  200.             if (FPageGet(i, &pPage, FALSE))
  201.                 delete pPage;
  202.             }
  203.  
  204.         SendMessage(m_hWndPageList, LB_RESETCONTENT, 0, 0L);
  205.         }
  206.  
  207.     if (NULL!=m_pIStorage)
  208.         m_pIStorage->Release();
  209.  
  210.     m_pIStorage=NULL;
  211.  
  212.     //If we're just cleaning up, then we're done.
  213.     if (NULL==pIStorage)
  214.         return TRUE;
  215.  
  216.     m_pIStorage=pIStorage;
  217.     m_pIStorage->AddRef();
  218.  
  219.     //If this is a new storage, create the streams we require
  220.     if (fInitNew)
  221.         {
  222.         //Page list header.
  223.         hr=m_pIStorage->CreateStream(SZSTREAMPAGELIST, dwMode | STGM_CREATE
  224.             , 0, 0, &pIStream);
  225.  
  226.         if (FAILED(hr))
  227.             return FALSE;
  228.  
  229.         pIStream->Release();
  230.  
  231.         //Device Configuration
  232.         hr=m_pIStorage->CreateStream(SZSTREAMDEVICECONFIG, dwMode | STGM_CREATE
  233.             , 0, 0, &pIStream);
  234.  
  235.         if (FAILED(hr))
  236.             return FALSE;
  237.  
  238.         pIStream->Release();
  239.         return TRUE;
  240.         }
  241.  
  242.  
  243.     /*
  244.      * We're opening an existing file:
  245.      *  1)  Configure for the device we're on
  246.      *  2)  Read the Page List and create page entries for each.
  247.      */
  248.  
  249.     ConfigureForDevice();
  250.  
  251.     //Read the page list.
  252.     hr=m_pIStorage->OpenStream(SZSTREAMPAGELIST, NULL, dwMode, 0, &pIStream);
  253.  
  254.     if (FAILED(hr))
  255.         return FALSE;
  256.  
  257.     if (SUCCEEDED(CoGetMalloc(MEMCTX_SHARED, &pIMalloc)))
  258.         {
  259.         pIStream->Read((LPVOID)&pgList, sizeof(PAGELIST), &cbRead);
  260.         m_cPages  =(UINT)pgList.cPages;
  261.         m_iPageCur=(UINT)pgList.iPageCur;
  262.         m_dwIDNext=pgList.dwIDNext;
  263.  
  264.         fRet=TRUE;
  265.         cbRead=pgList.cPages*sizeof(DWORD);
  266.  
  267.         if (0!=cbRead)
  268.             {
  269.             pdwID=(LPDWORD)pIMalloc->Alloc(cbRead);
  270.  
  271.             if (NULL!=pdwID)
  272.                 {
  273.                 pIStream->Read((LPVOID)pdwID, cbRead, &cbRead);
  274.  
  275.                 for (i=0; i < m_cPages; i++)
  276.                     fRet &=FPageAdd(-1, *(pdwID+i), FALSE); //-1==end of list
  277.  
  278.                 pIMalloc->Free((LPVOID)pdwID);
  279.                 }
  280.             }
  281.  
  282.         pIMalloc->Release();
  283.         }
  284.  
  285.     pIStream->Release();
  286.  
  287.     if (!fRet)
  288.         return FALSE;
  289.  
  290.     FPageGet(m_iPageCur, &pPage, TRUE);
  291.  
  292.     InvalidateRect(m_hWnd, NULL, FALSE);
  293.     UpdateWindow(m_hWnd);
  294.  
  295.     return TRUE;
  296.     }
  297.  
  298.  
  299.  
  300.  
  301.  
  302. /*
  303.  * CPages::FIStorageUpdate
  304.  *
  305.  * Purpose:
  306.  *  Insures that all pages are committed before a root save.
  307.  *
  308.  * Parameters:
  309.  *  fCloseAll       BOOL directing us to close all open storages and streams.
  310.  *
  311.  * Return Value:
  312.  *  BOOL            TRUE if successful, FALSE otherwise.
  313.  */
  314.  
  315. BOOL CPages::FIStorageUpdate(BOOL fCloseAll)
  316.     {
  317.     LPPAGE          pPage;
  318.     LPSTREAM        pIStream;
  319.     LPMALLOC        pIMalloc;
  320.     LPDWORD         pdwID;
  321.     ULONG           cb;
  322.     HRESULT         hr;
  323.     PAGELIST        pgList;
  324.     BOOL            fR